#include "vas_http.h"

namespace vas {


    struct VasHandler : public Pistache::Http::Handler {
    HTTP_PROTOTYPE(VasHandler)

        void onRequest(const Pistache::Http::Request &req, Pistache::Http::ResponseWriter writer) override {
        // TODO 实现HTTP功能
            try {
                auto path = req.resource();
                auto method = req.method();
                std::stringstream msg;
                msg << "get request " << path << " " << method;
                VAS_INFO(msg.str());
                writer.send(Pistache::Http::Code::Ok, "Hello, World!");
            }
            catch (std::exception &e) {
                VAS_ERROR("http handler get exception");
                VAS_ERROR(e.what());
            }
        }
    };


    Pistache::Http::Endpoint* StartHttpServer(int port, int threads) {
        Pistache::Address address(Pistache::Ipv4::any(), Pistache::Port(port));
        auto server = new Pistache::Http::Endpoint(address);
        auto opts = Pistache::Http::Endpoint::options()
                .threads(threads)
                .maxRequestSize(6553600);   // config the max size of request;
        // otherwise the sever is not able to process the reqest with an image

        server->init(opts);
        server->setHandler(Pistache::Http::make_handler<VasHandler>());
        server->serveThreaded();
        return server;
    }
}